package com.avalon.shop.controller;

import com.avalon.core.util.ObjectUtils;
import com.avalon.shop.service.PetService;
import com.avalon.shop.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("pet")
public class PetController {
    private final PetService petService;
    private final UserService userService;

    public PetController(PetService petService, UserService userService) {
        this.petService = petService;
        this.userService = userService;
    }

    @PostMapping("/category/get")
    public List<Map> getPetCategory() {
        return petService.getPetCategory();
    }

    @PostMapping("/small/type/get")
    public List<Map> getPetSmallCategory(@RequestBody Map<String, Object> param) {
        Integer bigTypeId = 0;
        if (param.containsKey("bigTypeId")) {
            bigTypeId = Integer.parseInt(param.get("bigTypeId").toString());
        }
        return petService.getPetSmallCategory(bigTypeId);
    }

    @PostMapping("/user/pets")
    public List<Map> getUserPets(@RequestBody Map<String, Object> param) {
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = Integer.parseInt(param.get("userId").toString());
        }
        String state = null;
        if (param.containsKey("state")) {
            state = param.get("state").toString();
        }
        return userService.getUserPets(userId, state);
    }

    @PostMapping("/user/pet/get")
    public Map getUserPet(@RequestBody Map<String, Object> param) {
        Integer petId = 0;
        if (param.containsKey("petId")) {
            petId = Integer.parseInt(param.get("petId").toString());
        }
        return petService.getUserPet(petId);
    }

    @PostMapping("/user/pet/add")
    public void addUserPet(@RequestBody Map<String, Object> param) {
        String name = "";
        if (param.containsKey("name")) {
            name = param.get("name").toString();
        }
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = Integer.parseInt(param.get("userId").toString());
        }
        Integer petCategoryId = 0;
        if (param.containsKey("petCategoryId")) {
            petCategoryId = Integer.parseInt(param.get("petCategoryId").toString());
        }
        Integer petSmallTypeId = 0;
        if (param.containsKey("petSmallTypeId") && ObjectUtils.isNotNull(param.get("petSmallTypeId"))) {
            petSmallTypeId = Integer.parseInt(param.get("petSmallTypeId").toString());
        }
        String sex = "";
        if (param.containsKey("sex")) {
            sex = param.get("sex").toString();
        }
        String avatar = "";
        if (param.containsKey("avatar")) {
            avatar = param.get("avatar").toString();
        }
        String birthday = "";
        if (param.containsKey("birthday")) {
            birthday = param.get("birthday").toString();
        }

        userService.addUserPet(name,
                userId,
                petCategoryId,
                petSmallTypeId,
                sex,
                avatar,
                birthday);
    }

    @PostMapping("/user/pet/update")
    public void updateUserPet(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        String name = "";
        if (param.containsKey("name")) {
            name = param.get("name").toString();
        }
        Integer userId = 0;
        if (param.containsKey("userId")) {
            userId = Integer.parseInt(param.get("userId").toString());
        }
        Integer petCategoryId = 0;
        if (param.containsKey("petCategoryId")) {
            petCategoryId = Integer.parseInt(param.get("petCategoryId").toString());
        }
        Integer petSmallTypeId = 0;
        if (param.containsKey("petSmallTypeId") && ObjectUtils.isNotNull(param.get("petSmallTypeId"))) {
            petSmallTypeId = Integer.parseInt(param.get("petSmallTypeId").toString());
        }
        String sex = "";
        if (param.containsKey("sex")) {
            sex = param.get("sex").toString();
        }
        String avatar = "";
        if (param.containsKey("avatar")) {
            avatar = param.get("avatar").toString();
        }
        String birthday = "";
        if (param.containsKey("birthday")) {
            birthday = param.get("birthday").toString();
        }

        userService.updateUserPet(id,
                name,
                userId,
                petCategoryId,
                petSmallTypeId,
                sex,
                avatar,
                birthday);
    }

    /**
     * 获取宠物疫苗名称
     *
     * @param param
     * @return
     */
    @PostMapping("/get/vaccination/all")
    public List<Map> getVaccinationAll(@RequestBody Map<String, Object> param) {
        Integer petBigTypeId = 0;
        if (param.containsKey("petBigTypeId")) {
            petBigTypeId = Integer.parseInt(param.get("petBigTypeId").toString());
        }
        return petService.getVaccinationAll(petBigTypeId);
    }


    @PostMapping("/get/vaccination/list")
    public List<Map> getPetVaccinationList(@RequestBody Map<String, Object> param) {
        Integer petId = 0;
        if (param.containsKey("petId")) {
            petId = Integer.parseInt(param.get("petId").toString());
        }
        return petService.getPetVaccinationList(petId);
    }


    @PostMapping("/add/vaccination")
    public void addPetVaccination(@RequestBody Map<String, Object> param) {
        Integer petId = null;
        String vaccinationDate = null;
        String vaccinationName = null;

        if (param.containsKey("petId")) {
            petId = Integer.parseInt(param.get("petId").toString());
        }
        if (param.containsKey("vaccinationDate")) {
            vaccinationDate = (String) param.get("vaccinationDate");
        }
        if (param.containsKey("vaccinationName")) {
            vaccinationName = (String) param.get("vaccinationName");
        }
        petService.addPetVaccination(petId, vaccinationDate, vaccinationName);
    }

    @PostMapping("/get/deworm/list")
    public List<Map> getPetDewormList(@RequestBody Map<String, Object> param) {
        Integer petId = 0;
        if (param.containsKey("petId")) {
            petId = Integer.parseInt(param.get("petId").toString());
        }
        return petService.getDewormList(petId);
    }


    @PostMapping("/add/deworm")
    public void addPetDeworm(@RequestBody Map<String, Object> param) {
        Integer petId = null;
        String dewormDate = null;
        String dewormKind = null;

        if (param.containsKey("petId")) {
            petId = Integer.parseInt(param.get("petId").toString());
        }
        if (param.containsKey("dewormDate")) {
            dewormDate = (String) param.get("dewormDate");
        }
        if (param.containsKey("dewormKind")) {
            dewormKind = (String) param.get("dewormKind");
        }
        petService.addPetDeworm(petId, dewormDate, dewormKind);
    }
}
